﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Honestysoft.Baseframework.Model;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.BaseObject;
using Honestysoft.Financing.Model;
using System.Data.Common;
using System.Web.SessionState;

namespace Honestysoft.Financing.WebUI.Module.Project.Json
{
    /// <summary>
    /// UploadProjectOverviewFile 的摘要说明
    /// </summary>
    public class UploadProjectOverviewFile : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            
            //同时保存到项目简介及最新的考察报告中(如果有考察报告的话)
            HttpPostedFile file = context.Request.Files["FileData"];
            string extName = file.FileName.Substring(file.FileName.LastIndexOf(".") + 1);
            string projectId = context.Request.Params["DataID"];//此处传入的是项目ID
            Honestysoft.BaseObject.BaseBusinessLogic<Sys_Attachment> attLogic = new BaseObject.BaseBusinessLogic<Sys_Attachment>();
            QueryParameters query = new QueryParameters();
            query.Add("DataID", projectId, QueryParameters.OperatorType.Equal);
            query.Add("AttachmentBuType", ((int)Honestysoft.Financing.Model.EnumItem.ProjectDocument.Overview).ToString(), QueryParameters.OperatorType.Equal);
            List<Sys_Attachment> lsOverviews = attLogic.GetEntitys(query);
            Bu_Investigate maxInvest = new Honestysoft.Financing.BusinessLogic.Project(Globals.CurrentConnStringName).GetMaxInvestigateByProject(projectId);//最后的现场考察报告

            DbTransaction transaction = Honestysoft.BaseLibrary.Data.DatabaseProvider.GetDbTransaction();
            DbConnection connection = transaction.Connection;
            try
            {

                if (lsOverviews != null && lsOverviews.Count == 1)
                {
                    lsOverviews[0].Size = file.ContentLength;
                    lsOverviews[0].Extension = extName;
                    attLogic.UpdateEntity(lsOverviews[0], transaction);
                    file.SaveAs(context.Server.MapPath(Globals.Home + Utils.Reports_Dir_Overview + "/" + lsOverviews[0].FileName + "." + extName));
                }
                if (maxInvest != null)//如果有现场考察报告
                {
                    query = new QueryParameters();
                    query.Add("DataID", maxInvest.ID, QueryParameters.OperatorType.Equal);
                    List<Sys_Attachment> lsInvest = attLogic.GetEntitys(query,transaction);
                    if (lsInvest != null && lsInvest.Count == 1)
                    {
                        lsInvest[0].Size = file.ContentLength;
                        lsInvest[0].Extension = extName;
                        attLogic.UpdateEntity(lsInvest[0], transaction);
                        file.SaveAs(context.Server.MapPath(Globals.Home + Utils.Reports_Dir_Investigate + "/" + lsInvest[0].FileName + "." + extName));
                    }
                }
                transaction.Commit();
                context.Response.Write("1");
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                Honestysoft.BaseLibrary.ExceptionHandling.ExceptionHelper.ExceptionDistribute(ex, false);
                Honestysoft.BaseLibrary.Logging.LoggingHelper.WriteLog(ex.Message);
                context.Response.Write("保存文件时出错！");
            }
            finally
            {
                connection.Close();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}